package api

import (
	"api-gateway/basic/inits"
	__ "api-gateway/basic/proto"
	"api-gateway/handler/request"
	"api-gateway/handler/response"
	"context"
	"github.com/gin-gonic/gin"
)

// EquipmentAdd
// @Summary      设备添加
// @Description  添加一个新的设备
// @Tags         设备模块
// @Accept       json
// @Produce      json
// @Param        equipment  body      request.EquipmentAdd  true  "设备参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Router       /equipment/equipmentAdd [post]
func EquipmentAdd(c *gin.Context) {
	var req request.EquipmentAdd
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}

	add, err := inits.UserCline.EquipmentAdd(context.Background(), &__.EquipmentAddReq{
		BrandId:        req.BrandId,
		EquipmentName:  req.EquipmentName,
		EquipmentModel: req.EquipmentModel,
		Manufacturer:   req.Manufacturer,
	})

	if err != nil {
		response.Error(c, 500, "设备添加失败", err.Error())
		return
	}
	response.Success(c, "设备添加成功", add.Id)
}

// EquipmentDel
// @Summary      设备删除
// @Description  删除一个设备
// @Tags         设备模块
// @Accept       json
// @Produce      json
// @Param        equipment  body      request.EquipmentDel  true  "设备参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Router       /equipment/equipmentDel [post]
func EquipmentDel(c *gin.Context) {
	var req request.EquipmentDel
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}

	_, err := inits.UserCline.EquipmentDel(context.Background(), &__.EquipmentDelReq{Id: req.Id})
	if err != nil {
		response.Error(c, 500, "设备添加失败", err.Error())
		return
	}
	response.Success(c, "设备删除成功", nil)
}

// EquipmentUpdate
// @Summary      修改设备
// @Description  删除一个设备
// @Tags         设备模块
// @Accept       json
// @Produce      json
// @Param        equipment  body      request.EquipmentUpdate  true  "设备参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Router       /equipment/equipmentUpdate [post]
func EquipmentUpdate(c *gin.Context) {
	var req request.EquipmentUpdate
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}

	update, err := inits.UserCline.EquipmentUpdate(context.Background(), &__.EquipmentUpdateReq{
		Id:             req.Id,
		BrandId:        req.BrandId,
		EquipmentName:  req.EquipmentName,
		EquipmentModel: req.EquipmentModel,
		Manufacturer:   req.Manufacturer,
	})
	if err != nil {
		response.Error(c, 500, "设备修改失败", err.Error())
		return
	}
	response.Success(c, "设备修改成功", update.Id)
}

// EquipmentFind
// @Summary      设备列表
// @Description  设备列表展示
// @Tags         设备模块
// @Accept       json
// @Produce      json
// @Param        equipment  query      request.EquipmentFind  true  "设备参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Router       /equipment/equipmentFind [get]
func EquipmentFind(c *gin.Context) {
	var req request.EquipmentFind
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}

	find, err := inits.UserCline.EquipmentFind(context.Background(), &__.EquipmentFindReq{})
	if err != nil {
		response.Error(c, 500, "设备列表查询失败", err.Error())
		return
	}
	response.Success(c, "设备列表查询成功", find)
}

// EquipmentDetails
// @Summary      设备详情
// @Description  详情列表展示
// @Tags         设备模块
// @Accept       json
// @Produce      json
// @Param        equipment  query      request.EquipmentDetails  true  "设备参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Router       /equipment/equipmentDetails [get]
func EquipmentDetails(c *gin.Context) {
	var req request.EquipmentDetails
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}

	details, err := inits.UserCline.EquipmentDetails(context.Background(), &__.EquipmentDetailsReq{Id: req.Id})
	if err != nil {
		response.Error(c, 500, "设备详情查询失败", err.Error())
		return
	}
	response.Success(c, "设备详情查询成功", details)
}
